//logs.js
const util = require('../../utils/util.js')
const { $Toast } = require('../../dist/base/index');
// const WxValidate = require('../../utils/WxValidate.js');
import WxValidate from '../../utils/WxValidate.js'
var api = require('../../config/api.js');
const app = getApp();

Page({
  data: {
    loading:false,

    sqrName:null,
    sqrLink:null,
    activityUnit:null,
    activityType:null,
    teacher:null,
    time:null,
    room: null,
    /*表单参数*/
  },
  onLoad: function (options) {
    let self = this;
    console.log(options)
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log(data)
      self.setData({
        room: data.data
      })
    })
    this.initValidate()
  },

  onShow: function () {
  },

  initValidate() {
    const rules = {
      sqrName: {
        required: true,
      },
      sqrLink:{
        required:true,
        tel:true
      },
      activityUnit:{
        required:true,
      },
      activityType:{
        required:true,
      },
      teacher:{
        required:true,
      },
      time:{
        required:true,
      }
    }
    const messages = {
      sqrName: {
        required: '请填写申请者',
      },
      sqrLink:{
        required:'请填写手机号',
        tel:'请填写正确的手机号'
      },

      activityUnit:{
        required:'请填写活动单位',
      },
      activityType:{
        required:'请填写活动类型',
      },
      teacher:{
        required:'请填写负责老师',
      },
      time:{
        required:'请填写使用时间段',
      }
    }
    this.WxValidate = new WxValidate(rules, messages)
  },

  inputSqrName(e){
    this.setData({
      sqrName: e.detail.value
    })
  },
  inputSqrLink(e){
    this.setData({
      sqrLink: e.detail.value
    })
  },
  inputActivityUnit(e){
    this.setData({
      activityUnit: e.detail.value
    })
  },
  inputActivityType(e){
    this.setData({
      activityType: e.detail.value
    })
  },
  inputTeacher(e){
    this.setData({
      teacher: e.detail.value
    })
  },

  inputTime(e){
    this.setData({
      time: e.detail.value
    })
  },

  formSubmit: function(e) {
    // debugger
    // console.log('form发生了submit事件，携带的数据为：', e.detail.value)
    // const params = e.detail.value
    // //校验表单
    // if (!this.WxValidate.checkForm(params)) {
    //   const error = this.WxValidate.errorList[0]
    //   this.showModal(error)
    //   return false
    // }
  },

  showModal(msg) {
    wx.showModal({
      content: msg,
      showCancel: false,
    })
  },

  submit(e){
    let self = this;
    if (self.data.sqrName == null || self.data.sqrName == ""){
      this.showModal("请填写申请者")
      return
    }
    if (!/^1[34578]\d{9}$/.test(self.data.sqrLink)){
      this.showModal("请输入正确手机号")
      return
    }
    if (self.data.activityUnit == null || self.data.activityUnit == ""){
      this.showModal("请填写活动单位")
      return
    }
    if (self.data.activityType == null || self.data.activityType == ""){
      this.showModal("请填写活动类型")
      return
    }
    if (self.data.teacher == null || self.data.teacher == ""){
      this.showModal("请填写负责老师")
      return
    }
    if (self.data.time == null || self.data.time == ""){
      this.showModal("请填写使用时间段")
      return
    }

    this.setData({
      loading: true
    })
    util.request(api.SubmitDmt, {
      sqrName: self.data.sqrName,
      sqrLink: self.data.sqrLink,
      activityUnit: self.data.activityUnit,
      activityType: self.data.activityType,
      teacher: self.data.teacher,
      time: self.data.time,
      roomId: self.data.room.id
    }, 'POST').then(res => {
      $Toast({
        content: res.msg,
        type: 'success'
      });
      if (res.success) {
        this.setData({
          loading: false
        })
        wx.navigateBack();
      }
    }).catch(err => {
      this.setData({
        loading: false
      })
    });
  }

})
